﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Appraisal_System.Common
{
    public class FormFactory<T>
    {
        //(单列） 避免切换窗体时，窗体闪烁
        /*private static FrmUserManager frmUserManager;
        private static FrmBaseManager frmBaseManager;
        private static FrmNone frmNone;*/
        private static Form form;
        private static List<Form> forms = new List<Form>();

        /*   /// <summary>
           /// 窗体的切换
           /// </summary>
           /// <param name="index">主窗体选择的第几项（0，1，2）</param>
           /// <returns></returns>
           public static Form CreateFactory(int index)
           {
               HideFormAll();
               switch (index)
               {
                   //用户管理
                   case 0:
                       if (frmUserManager == null)
                       {
                           frmUserManager = new FrmUserManager();
                           forms.Add(frmUserManager);
                       }
                       form = frmUserManager;
                       break;
                   //基数管理
                   case 1:
                       if (frmBaseManager == null)
                       {
                           frmBaseManager = new FrmBaseManager();
                           forms.Add(frmBaseManager);
                       }
                       form = frmBaseManager;
                       break;
                   default:
                       if (frmNone == null)
                       {
                           frmNone = new FrmNone();
                           forms.Add(frmNone);
                       }
                       form = frmNone;
                       break;
               }
               return form;
           }
   */

        private static List<Type> types;
        static  FormFactory()
        {
            Assembly assembly = Assembly.LoadFrom("Appraisal_System.exe");
             types = assembly.GetTypes().ToList();
        }
        public static Form CreateFactory(String formName)
        {
            
            //三种加载程序集的方法
            /* Assembly assembly = Assembly.Load("Appraisal_System");*/

            /*  string path = AppDomain.CurrentDomain.BaseDirectory;
              Assembly assembly = Assembly.LoadFile(path+"Appraisal_System.exe");*/

            /*Assembly assembly = Assembly.LoadFrom("Appraisal_System.exe");*/

           //List<Type> types = assembly.GetTypes().ToList();
            HideFormAll();

            //避免多次实例化窗口
            Form form = forms.Find(m => m.Name == formName);
            if(form == null)
            {
                Type type = types.Find(m => m.Name == formName);
                form = (Form)Activator.CreateInstance(type);
                forms.Add(form);
            }
            return form;
        }

        /// <summary>
        /// 关闭所有窗体控件
        /// </summary>
        public static void HideFormAll()
        {
            /* if(frmBaseManager != null)
             {
                 frmBaseManager.Hide();
             }*/
            //加 ? 等于上述注释信息
            /*  frmBaseManager?.Hide();
              frmUserManager?.Hide();*/
            foreach (Form form in forms)
            {
                form.Hide();
            }
        }
    }
}
